import geom.FacetList;
import geom.LineSet;
import geom.Polygon;
import geom.PolygonList;
import geom.Solid;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Random;

import javax.media.j3d.BoundingBox;
import javax.vecmath.Point3d;

import nativ.ContourGenerator;

import com.sun.j3d.loaders.IncorrectFormatException;
import com.sun.j3d.loaders.ParsingErrorException;



public class test {

	/**
	 * @param args
	 * @throws ParsingErrorException 
	 * @throws IncorrectFormatException 
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException, IncorrectFormatException, ParsingErrorException
	{
	    /*test3();
	    Polygon poly = new Polygon(new double[][] { {5, 10}, { 0, 0 }, { 0, 10 }});
	    
	    System.out.println(poly.getWindingDirection());*/
        
        //test0();
		
		Polygon Ashape = new Polygon(new double[][]{ {0,10}, {1,10}, {3,7}, {8,7}, {9,10},{10,10},{5.5,0},{4.5,0}});
		Polygon Ahole  = new Polygon(new double[][]{ {3,6}, {5,1}, {7,6}});    

		PolygonList list = new PolygonList();		
		
		list.add(Ashape);
		//list.add(Ahole);
		
		PolygonList inset = ContourGenerator.generateContour(list);

		System.out.println(list.get(0).getArea());
		System.out.println(inset.get(0).getArea());
	}
	
	public static void test0()
	{
		Random r = new Random();
		
		for (int i = 0; i < 200; i++)
		{
			int base = Math.abs(r.nextInt());
			/*for (int j = 0; j < 10; j++)
			{
				//int next = r.nextInt(5000);
				int next = 0;
				System.out.println(base + next);
			}*/
			
			System.out.println(base);
		}
	}
	
	public static void test1() throws FileNotFoundException, IncorrectFormatException, ParsingErrorException
	{
		StlFile loader = new StlFile();
		Solid scene = loader.load("Andy.stl");
		BoundingBox bounds = (BoundingBox) scene.getBounds();
		
		Point3d lower = new Point3d(), upper = new Point3d();
		
		bounds.getLower(lower);
		bounds.getUpper(upper);
		
		System.out.println(bounds);
		
		FacetList fl = loader.generateFacetList();
			
		//TODO scale object so it always fits...translate too
		
		//Bounding box: 0,0,0 -> 50,50,50
		
		fl.standardize(bounds);
		
		double xLen = Math.abs(upper.x - lower.x);
		double yLen = Math.abs(upper.y - lower.y);
		
		//fl.getSliceForZ(0.1);
		
		new SliceDisplay(fl, scene, lower.z, upper.z, Math.max(xLen, yLen)).setVisible(true);			
	}
	
	public static void test2() throws FileNotFoundException, IncorrectFormatException, ParsingErrorException
	{
	    StlFile loader = new StlFile();
		Solid scene = loader.load("r.stl");
		BoundingBox bounds = (BoundingBox) scene.getBounds();
		
		Point3d lower = new Point3d(), upper = new Point3d();
		
		bounds.getLower(lower);
		bounds.getUpper(upper);
		
		System.out.println(bounds);
		
		FacetList fl = loader.generateFacetList();
			
		//TODO scale object so it always fits...translate too
		
		//Bounding box: 0,0,0 -> 50,50,50
		
		fl.standardize(bounds);
		
		double xLen = Math.abs(upper.x - lower.x);
		double yLen = Math.abs(upper.y - lower.y);
		
		//fl.getSliceForZ(0.1);
		
		double minZ = lower.z, maxZ = upper.z;

		System.out.println("Starting test");
		for (int i = 0; i < 1000; i++)
		{
		    double zVal = minZ + ((maxZ - minZ) / 999 * i);
		    
		    ArrayList<LineSet> slices = fl.getLineSetsForZ(zVal);
		    
		    if (slices.size() > 13)
		    {
		//	System.out.println("z: " + zVal);
        		    for (LineSet set : slices)
        		    {
        	//		System.out.println("\t" + set);
        		    }
		    }
		}
		
		System.out.println("Test ended");
	}
}
